﻿using SmartRS.com.vn.smartrs.domain;
using SmartRS.com.vn.smartrs.service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmartRS.com.vn.smartrs.controller
{
    class FoodOrderController
    {
        public static FoodOrder getFoodOrderFromListById(IList<FoodOrder> listFoodOrder, long id)
        {
            try
            {
                foreach (FoodOrder foodOrder in listFoodOrder)
                {
                    if (foodOrder.foodOrderId == id)
                    {
                        return foodOrder;
                    }
                }
                return null;
            }
            catch(Exception ex)
            {
                throw new Exception("getFoodOrderFromListById happened error: " + ex.Message);
            }

        }

        public static long calculatorCharge(IList<FoodOrder> listFoodOrder, int VAT)
        {
            if (listFoodOrder == null || listFoodOrder.Count <= 0)
            {
                return 0L;
            }
            long priceWithoutVAT = calculatorPriceWithoutVAT(listFoodOrder);
            long priceBase = calculatorBasePrice(listFoodOrder);
            //Cash will charge customer
            long result = priceWithoutVAT + ((VAT / 100) * priceBase);

            return result;
        }

        public static long calculatorPriceWithoutVAT(IList<FoodOrder> listFoodOrder)
        {
            if (listFoodOrder == null || listFoodOrder.Count <= 0)
            {
                return 0L;
            }

            //Cash minus discount, and without VAT
            return calculatorBasePrice(listFoodOrder) - calculatorDiscountTotal(listFoodOrder);
        }

        public static long calculatorBasePrice(IList<FoodOrder> listFoodOrder)
        {
            if (listFoodOrder == null || listFoodOrder.Count <= 0)
            {
                return 0L;
            }
            long priceTotal = 0;
            foreach (FoodOrder foodOrder in listFoodOrder)
            {
                priceTotal += (foodOrder.priceWhenOrder * foodOrder.quantityOrder);
            }

            return priceTotal;
        }

        private static long calculatorDiscountTotal(IList<FoodOrder> listFoodOrder)
        {
            if (listFoodOrder == null || listFoodOrder.Count <= 0)
            {
                return 0L;
            }

            long discountTotal = 0;
            foreach (FoodOrder foodOrder in listFoodOrder)
            {
                discountTotal += foodOrder.discountValue;
            }

            return discountTotal;
        }
        public static bool checkFoodOrder(long foodId, long billId, ref FoodOrder foodOrder)
        {
            try
            {
                foodOrder = FoodOrderService.getInstance().getFoodOrder(billId, foodId);
                if (foodOrder != null)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi kết nối cơ sở dữ liệu: " + ex.Message);
                foodOrder = null;
                throw new Exception("checkFoodOrder happened error: " + ex.Message);
            }
            foodOrder = null;
            return false;
        }
        

    }
}
